<%@ page import="com.example.util.DBUtil" %>
<%@ page import="com.example.domain.User" %>
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>购物车调试</title>
</head>
<body>
    <h1>购物车功能调试</h1>
    
    <%
        // 检查用户是否已登录
        User currentUser = (User) session.getAttribute("user");
        if (currentUser == null) {
    %>
        <p>用户未登录</p>
        <a href="../auth/login.jsp">前往登录</a>
    <%
        } else {
    %>
        <p>当前用户ID: <%= currentUser.getId() %></p>
        <p>当前用户名: <%= currentUser.getUsername() %></p>
        
        <h2>测试添加商品到购物车</h2>
        <form method="post">
            <label>商品ID: <input type="number" name="productId" value="1" required></label><br><br>
            <label>数量: <input type="number" name="quantity" value="1" required></label><br><br>
            <input type="submit" value="添加到购物车">
        </form>
        
        <%
            if ("POST".equalsIgnoreCase(request.getMethod())) {
                Connection conn = null;
                try {
                    int productId = Integer.parseInt(request.getParameter("productId"));
                    int quantity = Integer.parseInt(request.getParameter("quantity"));
                    
                    out.println("<h3>调试信息:</h3>");
                    out.println("<p>商品ID: " + productId + "</p>");
                    out.println("<p>数量: " + quantity + "</p>");
                    
                    conn = DBUtil.getConnection();
                    out.println("<p style='color: green;'>1. 数据库连接成功</p>");
                    
                    // 检查商品是否存在
                    String productSql = "SELECT id, product_name, price, sale_price FROM tb_product WHERE id = ?";
                    PreparedStatement productStmt = conn.prepareStatement(productSql);
                    productStmt.setInt(1, productId);
                    ResultSet productRs = productStmt.executeQuery();
                    
                    if (productRs.next()) {
                        String productName = productRs.getString("product_name");
                        double price = productRs.getDouble("price");
                        double salePrice = productRs.getDouble("sale_price");
                        double productPrice = (salePrice > 0) ? salePrice : price;
                        
                        out.println("<p style='color: green;'>2. 商品存在</p>");
                        out.println("<p>商品名称: " + productName + "</p>");
                        out.println("<p>商品价格: " + price + "</p>");
                        out.println("<p>促销价格: " + salePrice + "</p>");
                        out.println("<p>实际价格: " + productPrice + "</p>");
                        
                        // 检查是否已在购物车中
                        String checkSql = "SELECT id, sale_num FROM tb_cart WHERE member_id = ? AND product_id = ?";
                        PreparedStatement checkStmt = conn.prepareStatement(checkSql);
                        checkStmt.setInt(1, currentUser.getId());
                        checkStmt.setInt(2, productId);
                        ResultSet rs = checkStmt.executeQuery();
                        
                        if (rs.next()) {
                            // 更新购物车
                            int cartId = rs.getInt("id");
                            int currentQuantity = rs.getInt("sale_num");
                            out.println("<p style='color: blue;'>3. 商品已在购物车中，更新数量</p>");
                            out.println("<p>购物车ID: " + cartId + "</p>");
                            out.println("<p>当前数量: " + currentQuantity + "</p>");
                            
                            String updateSql = "UPDATE tb_cart SET sale_num = ?, sale_price = ?, sale_money = ? WHERE id = ?";
                            PreparedStatement updateStmt = conn.prepareStatement(updateSql);
                            updateStmt.setInt(1, currentQuantity + quantity);
                            updateStmt.setDouble(2, productPrice);
                            updateStmt.setDouble(3, productPrice * (currentQuantity + quantity));
                            updateStmt.setInt(4, cartId);
                            
                            int updated = updateStmt.executeUpdate();
                            out.println("<p style='color: green;'>4. 更新结果: " + updated + " 行受影响</p>");
                        } else {
                            // 添加到购物车
                            out.println("<p style='color: blue;'>3. 商品不在购物车中，添加新记录</p>");
                            
                            String insertSql = "INSERT INTO tb_cart (member_id, product_id, sale_num, sale_price, sale_money) VALUES (?, ?, ?, ?, ?)";
                            PreparedStatement insertStmt = conn.prepareStatement(insertSql);
                            insertStmt.setInt(1, currentUser.getId());
                            insertStmt.setInt(2, productId);
                            insertStmt.setInt(3, quantity);
                            insertStmt.setDouble(4, productPrice);
                            insertStmt.setDouble(5, productPrice * quantity);
                            
                            int inserted = insertStmt.executeUpdate();
                            out.println("<p style='color: green;'>4. 插入结果: " + inserted + " 行受影响</p>");
                        }
                        
                        out.println("<p style='color: green; font-weight: bold;'>成功添加商品到购物车!</p>");
                    } else {
                        out.println("<p style='color: red;'>错误: 商品不存在</p>");
                    }
                } catch (Exception e) {
                    out.println("<p style='color: red;'>错误: " + e.getMessage() + "</p>");
                    e.printStackTrace();
                } finally {
                    if (conn != null) {
                        try {
                            conn.close();
                        } catch (Exception e) {
                            // 忽略关闭连接的异常
                        }
                    }
                }
            }
        %>
    <%
        }
    %>
    
    <h2>检查购物车内容</h2>
    <a href="view_cart.jsp">查看购物车</a>
</body>
</html>